package com.irdstudio.efp.flow.api.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.*;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.basic.framework.core.vo.EngineHttpRequestVO;
import com.irdstudio.basic.framework.web.util.HttpClientUtil;
import com.irdstudio.basic.sequence.service.facade.SequenceService;
import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.console.service.facade.PubSysInfoService;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.efp.flow.common.constance.FlowConstance;
import com.irdstudio.efp.flow.executor.util.AbstractController;
import com.irdstudio.efp.flow.service.FlowCommonService;
import com.irdstudio.efp.flow.service.FlowRepulseUtil;
import com.irdstudio.efp.flow.service.facade.BizFlowInfoService;
import com.irdstudio.efp.flow.service.facade.FlowUrlConfigService;
import com.irdstudio.efp.flow.service.vo.BizFlowInfoVO;
import com.irdstudio.efp.loan.service.facade.AccLoanService;
import com.irdstudio.efp.loan.service.vo.AccLoanVO;
import com.irdstudio.efp.riskm.common.RiskcataConstant;
import com.irdstudio.efp.riskm.service.facade.RscAdjustAppService;
import com.irdstudio.efp.riskm.service.facade.RscTaskLoanService;
import com.irdstudio.efp.riskm.service.vo.RscAdjustAppVO;
import com.irdstudio.efp.riskm.service.vo.RscAdjustContVO;
import com.irdstudio.efp.riskm.service.vo.RscTaskLoanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;


@RestController
@RequestMapping("/api")
public class RscAdjustAppController extends AbstractController {

	@Autowired
	@Qualifier("rscAdjustAppService")
	private RscAdjustAppService rscAdjustAppService;

	@Autowired
	@Qualifier("dbSeqService")
	private SequenceService sequenceService;

	@Autowired
	@Qualifier("bizFlowInfoService")
	private BizFlowInfoService bizFlowInfoService;

	@Autowired
	@Qualifier("pubSysInfoService")
	private PubSysInfoService pubSysInfoService;

	@Autowired
	@Qualifier("flowUrlConfigService")
	private FlowUrlConfigService flowUrlConfigService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	@Autowired
	private FlowCommonService flowCommonService;

	@Autowired
	@Qualifier("flowRepulseUtil")
	private FlowRepulseUtil flowRepulseUtil;

	@Autowired
	@Qualifier("rscTaskLoanService")
	private RscTaskLoanService rscTaskLoanService;

	@Autowired
	@Qualifier("accLoanService")
	private AccLoanService accLoanService;
	/**
	 * 风险分类调整列表数据查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/rsc/adjust/apps", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<RscAdjustAppVO>> queryRscAdjustAppAll(@RequestBody RscAdjustAppVO vo) {
		List<RscAdjustAppVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		if (!StringUtil.isNullorBank(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())) {
			logger.info("开始导出数据！");
			outputVo = queryExByDataRuleType(dataRuleTypeMap,vo);
			addVaildDate(outputVo);
		} else {
			logger.info("开始查询数据！");
			outputVo = queryByDataRuleType(dataRuleTypeMap,vo);
		}
		return getResponseData(outputVo);
	}

	/**
	 * 风险分类调整解除列表数据查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/rsc/adjust/remove/apps", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<RscAdjustAppVO>> queryRscAdjustRemoveApp(@RequestBody RscAdjustAppVO vo) {
		List<RscAdjustAppVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		if (!StringUtil.isNullorBank(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())) {
			logger.info("开始导出数据！");
			outputVo = queryExByDataRuleType(dataRuleTypeMap,vo);
			addVaildDate(outputVo);
		} else {
			logger.info("开始查询数据！");
			outputVo = queryByDataRuleType(dataRuleTypeMap,vo);
		}
		return getResponseData(outputVo);
	}

	private List<RscAdjustAppVO> queryByDataRuleType(Map<String, String> dataRuleTypeMap, RscAdjustAppVO vo){
		List<RscAdjustAppVO> outputVo = null;
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
			case Owner:
				outputVo = rscAdjustAppService.queryAllOwner(vo);
				break;
			case CurrOrg:
				outputVo = rscAdjustAppService.queryAllCurrOrg(vo);
				break;
			case CurrDownOrg:
				outputVo = rscAdjustAppService.queryAllCurrDownOrg(vo);
				break;
			case CurrOwnerPrd:
				outputVo = rscAdjustAppService.queryAllCurrOwnerPrd(vo);
				break;
			default:
				outputVo = rscAdjustAppService.queryAllOwner(vo);
				break;
		}
		flowCommonService.displayOrgName(outputVo,"inputBrId","inputBrName");
		flowCommonService.displayOrgName(outputVo,"mainBrId","mainBrName");
		return outputVo;
	}

	private List<RscAdjustAppVO> queryExByDataRuleType(Map<String, String> dataRuleTypeMap, RscAdjustAppVO vo){
		List<RscAdjustAppVO> outputVo = null;
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
			case Owner:
				outputVo = rscAdjustAppService.queryExAllOwner(vo);
				break;
			case CurrOrg:
				outputVo = rscAdjustAppService.queryExAllCurrOrg(vo);
				break;
			case CurrDownOrg:
				outputVo = rscAdjustAppService.queryExAllCurrDownOrg(vo);
				break;
			case CurrOwnerPrd:
				outputVo = rscAdjustAppService.queryExAllCurrOwnerPrd(vo);
				break;
			default:
				outputVo = rscAdjustAppService.queryExAllOwner(vo);
				break;
		}
		flowCommonService.displayOrgName(outputVo,"inputBrId","inputBrName");
		flowCommonService.displayOrgName(outputVo,"mainBrId","mainBrName");
		return outputVo;
	}


	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/rsc/adjust/app", method=RequestMethod.GET)
	public @ResponseBody ResponseData<RscAdjustAppVO> queryByPk() {
		RscAdjustAppVO inVo = new RscAdjustAppVO();
		RscAdjustAppVO outputVo = rscAdjustAppService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * TODO: 根据分类调整流水号 删除 风险分类调整申请表数据
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/5 15:33
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/app/rscAdjNo", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByRscAdjNo(@RequestBody RscAdjustAppVO inRscAdjustAppVo) {
		int outputVo = rscAdjustAppService.deleteByRscAdjNo(inRscAdjustAppVo);
		return getResponseData(outputVo);

	}

	/**
	 * TODO: 根据分类调整流水号 删除 风险分类调整申请表数据
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/5 15:33
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/app/rscRmNo", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByRscRmNo(@RequestBody RscAdjustAppVO inRscAdjustAppVo) {
		int outputVo = rscAdjustAppService.deleteByRscRmNo(inRscAdjustAppVo);
		return getResponseData(outputVo);

	}

	/**
	 * TODO: 根据分类调整流水号 修改 风险分类调整申请表数据
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/5 15:33
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/app/rscAdjNo", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByRscAdjNo(@RequestBody RscAdjustAppVO inRscAdjustAppVo) {
		inRscAdjustAppVo.setAprvUserCode("");
		inRscAdjustAppVo.setAprvComment("");
		inRscAdjustAppVo.setApprDate("");
		int outputVo = 0;
		inRscAdjustAppVo.setCurrResult(queryCurrResult(inRscAdjustAppVo.getBillNo()));
		if (inRscAdjustAppVo.getAdjustBasic().equals(inRscAdjustAppVo.getAdjustResult()) || inRscAdjustAppVo.getAdjustResult().equals(inRscAdjustAppVo.getCurrResult())){
			outputVo = -2;
			return setResponseMessage(outputVo,"当前调整前后结果一致，属无效业务!");
		}
		outputVo = rscAdjustAppService.updateByRscAdjNo(inRscAdjustAppVo);
		return getResponseData(outputVo);
	}

	/**
	 * TODO: 根据解除分类调整流水号 修改 风险分类调整申请表数据
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/6 10:03
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/app/rscRmNo", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByRscRmNo(@RequestBody RscAdjustAppVO inRscAdjustAppVo) {
		inRscAdjustAppVo.setAprvUserCode("");
		inRscAdjustAppVo.setAprvComment("");
		inRscAdjustAppVo.setApprDate("");
		int outputVo = rscAdjustAppService.updateByRscRmNo(inRscAdjustAppVo);
		return getResponseData(outputVo);
	}

	/**
	 * TODO: 新增风险分类调整申请表数据 --分类
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/5 15:33
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/app/rscAdjNo", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertRscAdjNo(@RequestBody RscAdjustAppVO inRscAdjustAppVo) {
		int outputVo = 0;
		setUserInfoToVO(inRscAdjustAppVo);
		List<RscAdjustAppVO> qsoutputVo = rscAdjustAppService.queryByBillNo(inRscAdjustAppVo);
		inRscAdjustAppVo.setCurrResult(queryCurrResult(inRscAdjustAppVo.getBillNo()));
		if (inRscAdjustAppVo.getAdjustBasic().equals(inRscAdjustAppVo.getAdjustResult()) || inRscAdjustAppVo.getAdjustResult().equals(inRscAdjustAppVo.getCurrResult())){
			outputVo = -2;
			return setResponseMessage(outputVo,"当前调整前后结果一致，属无效业务!");
		}
		if (qsoutputVo.size()>0){
			for (RscAdjustAppVO item : qsoutputVo) {
				if (ConsoleConstant.APRV_STATUS_01.equals(item.getApprStatus()) || ConsoleConstant.APRV_STATUS_02.equals(item.getApprStatus())  || ConsoleConstant.APRV_STATUS_05.equals(item.getApprStatus())) {
					outputVo = -1;
					return setResponseMessage(outputVo, "该笔借据存在未处理完成的调整申请！");
				}
			}
		}
		AccLoanVO accLoanVO = new AccLoanVO();
		accLoanVO.setBillNo(inRscAdjustAppVo.getBillNo());
		if (null == accLoanService.queryByPk(accLoanVO)){
			outputVo = -3;
			return setResponseMessage(outputVo, "该笔借据不存在，无法发起申请！");
		}
		inRscAdjustAppVo.setCreateUser(inRscAdjustAppVo.getLoginUserId());
		inRscAdjustAppVo.setInputBrId(inRscAdjustAppVo.getLoginUserOrgCode());
		inRscAdjustAppVo.setCreateTime(TimeUtil.getCurrentDateTime());
		inRscAdjustAppVo.setLastUpdateUser(inRscAdjustAppVo.getLoginUserId());
		inRscAdjustAppVo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
		// 审批状态 = 待发起
		inRscAdjustAppVo.setApprStatus(ConsoleConstant.APRV_STATUS_01);
		// 生成方式 = 人工认定
		inRscAdjustAppVo.setGeneWay(RiskcataConstant.S_ZB_GENE_WAY_2);
		//  分类类型 = 调整
		inRscAdjustAppVo.setTaskType(RiskcataConstant.S_ZB_TASK_TYPE_1);
		try {
			// 风险分类流水号
			String seq = sequenceService.getSequence("RSCADAPP_SEQ_ID","null","null");
			inRscAdjustAppVo.setRscAdjNo(seq);
			outputVo = rscAdjustAppService.insertRscAdjustApp(inRscAdjustAppVo);
		} catch (Exception e) {
			logger.error("********************************************插入数据出错，异常信息为：", e.getMessage());
			outputVo = -1;
			return setResponseMessage(outputVo, "插入数据出错！");
		}
		return getResponseData(outputVo);

	}

	/**
	 * TODO: 新增风险分类调整申请表数据 --解除
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/6 9:42
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/app/rscRmNo", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertRscRmNo(@RequestBody RscAdjustAppVO inRscAdjustAppVo) {
		int outputVo = 0;
		setUserInfoToVO(inRscAdjustAppVo);
		List<RscAdjustAppVO> qsoutputVo = rscAdjustAppService.queryByBillNo(inRscAdjustAppVo);
		if (qsoutputVo.size()>0){
			for (RscAdjustAppVO item : qsoutputVo) {
				if (ConsoleConstant.APRV_STATUS_01.equals(item.getApprStatus()) || ConsoleConstant.APRV_STATUS_02.equals(item.getApprStatus())  || ConsoleConstant.APRV_STATUS_05.equals(item.getApprStatus())) {
					outputVo = -1;
					return setResponseMessage(outputVo, "该笔借据存在未处理完成的调整申请！");
				}
			}
		}
		inRscAdjustAppVo.setCreateUser(inRscAdjustAppVo.getLoginUserId());
		inRscAdjustAppVo.setInputBrId(inRscAdjustAppVo.getLoginUserOrgCode());
		inRscAdjustAppVo.setCreateTime(TimeUtil.getCurrentDateTime());
		inRscAdjustAppVo.setLastUpdateUser(inRscAdjustAppVo.getLoginUserId());
		inRscAdjustAppVo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
		// 审批状态 = 待发起
		inRscAdjustAppVo.setApprStatus(ConsoleConstant.APRV_STATUS_01);
		// 生成方式 = 人工解除
		inRscAdjustAppVo.setGeneWay(RiskcataConstant.S_ZB_GENE_WAY_3);
		//  分类类型 = 解除
		inRscAdjustAppVo.setTaskType(RiskcataConstant.S_ZB_TASK_TYPE_2);
		//  调整后分类结果 = 机评分类结果
		inRscAdjustAppVo.setAdjustResult(inRscAdjustAppVo.getAdjustBasic());
		try {
			// 风险分类流水号
			String seq = sequenceService.getSequence("RSCRMAPP_SEQ_ID","null","null");
			inRscAdjustAppVo.setRscRmNo(seq);
			outputVo = rscAdjustAppService.insertRscAdjustApp(inRscAdjustAppVo);
		} catch (Exception e) {
			logger.error("********************************************插入数据出错，异常信息为：", e.getMessage());
			outputVo = -1;
		}


		return getResponseData(outputVo);

	}

	/**
	 * TODO: 查询 审批状态 = 通过 数据
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/6 15:05
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/apps/appr", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<RscAdjustAppVO>> queryAllAppr(@RequestBody RscAdjustAppVO vo) {
		List<RscAdjustAppVO> outputVo = rscAdjustAppService.queryAllAppr(vo);
		flowCommonService.displayOrgName(outputVo,"mainBrId","mainBrName");
		flowCommonService.displayOrgName(outputVo,"inputBrId","inputBrName");
		return getResponseData(outputVo);

	}

	/**
	 * TODO: 导出数据
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/6 15:06
	 * @version 0.1
	 */
	private List<RscAdjustAppVO> addVaildDate(List<RscAdjustAppVO> outputVo) {
		if (outputVo.size()>0){
			for (RscAdjustAppVO item : outputVo) {
				// 生效日期 = （通过）审批日期
				if(item.getApprStatus().equals(ConsoleConstant.APRV_STATUS_03)){
					item.setVaildDate(item.getApprDate());
				}else{
					item.setVaildDate("");
				}
			}
		}
		return outputVo;

	}


	/**
	 * TODO: 根据风险分类调整流水号查询表
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/17 14:59
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/apps/adjno", method=RequestMethod.POST)
	public @ResponseBody ResponseData<RscAdjustAppVO> queryByAdjNo(@RequestBody RscAdjustAppVO vo) {
		RscAdjustAppVO outputVo = rscAdjustAppService.queryByAdjNo(vo);
		return getResponseData(outputVo);
	}

	/**
	 * TODO: 根据风险分类调整流水号查询表
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/17 14:59
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/apps/rmno", method=RequestMethod.POST)
	public @ResponseBody ResponseData<RscAdjustAppVO> queryByRmNo(@RequestBody RscAdjustAppVO vo) {
		RscAdjustAppVO outputVo = rscAdjustAppService.queryByRmNo(vo);
		return getResponseData(outputVo);
	}

	/**
	 * TODO: 风险分类调整解除 提交数据 发起流程
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/11 10:25
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/rm/commit", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> rmCommit(@RequestBody RscAdjustAppVO vo) {
		int outputVo = 0;
		setUserInfoToVO(vo);
		ResponseData<Integer> integerResponseData =null;
		if(null==vo.getRscRmNo() || ""==vo.getRscRmNo()){
			// 新增数据
			integerResponseData = insertRscRmNo(vo);
		}else{
			// 修改数据
			integerResponseData = updateByRscRmNo(vo);
		}
		if(!integerResponseData.getCode().equals("0")){
			outputVo = -1;
			return setResponseMessage(outputVo, integerResponseData.getMessage());
		}
		if(vo.getApprStatus().equals(ConsoleConstant.APRV_STATUS_05)){
			flowRepulseUtil.cleanRepulseAllInfo(vo.getRscRmNo());
		}
		// 发起流程
		logger.info("即将发起额度限额审批流程，信息为:" + JSON.toJSONString(vo));
		// 事件ID
		String eventId = "mRscAdjustRemoveEnter";
		String flowId = "FW20190910000000002";
		vo.setApprStatus(ConsoleConstant.APRV_STATUS_02);
		BizFlowInfoVO bizFlowInfoVO = new BizFlowInfoVO();
		bizFlowInfoVO.setBizFlowType(FlowConstance.BIZ_FLOW_TYPE_09);
		bizFlowInfoVO.setBizFlowId(flowId);
		List<BizFlowInfoVO> listFlow = bizFlowInfoService.queryBizFlowInfoByFlowType(bizFlowInfoVO);

		logger.info("查询到的业务流程参数为："+ JSONObject.toJSONString(listFlow));
		if (Objects.nonNull(listFlow) && listFlow.size()>0) {
			bizFlowInfoVO = listFlow.get(0);
			flowId = bizFlowInfoVO.getBizFlowId();  // 得到业务流转ID
		} else {
			//提交失败 待发起
			vo.setApprStatus(ConsoleConstant.APRV_STATUS_01);
			rscAdjustAppService.updateByRscRmNo(vo);
			outputVo = -1;
			return setResponseMessage(outputVo, "未配置相关信息提交失败!");
		}
		EngineHttpRequestVO eVo = new EngineHttpRequestVO();
		// 业务法人机构
		eVo.setBizLagelOrgCode(this.getUserInfo().getOrgInfo().getOrgCode());
		// 业务流水号
		eVo.setE_bizSerno(vo.getRscRmNo());
		eVo.setFlowId(flowId);

		//事件ID
		eVo.setData(vo);
		eVo.setEventId(eventId);
		eVo.setG_bizSerno(vo.getRscRmNo());
		if (!flowCommonService.dutyFlowCheck(eventId,flowId)){
			outputVo = -1;
			return setResponseMessage(outputVo, "发起风险分类调整解除申请审批流程失败!原因: 流程岗位已失效");
		}
		try {
			HttpClientUtil.callFlowEngine(flowUrlConfigService.getRestfulSyncStartEventUrl(), eVo, String.class);
			//更新 申请日期
			vo.setAppDate(pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001));
			outputVo = rscAdjustAppService.updateByRscRmNo(vo);
		} catch (Exception e) {
			// 发起失败
			e.printStackTrace();
			logger.error("RscAdjustAppVO", e.getMessage());
			outputVo = -1;
			return setResponseMessage(outputVo, "发起风险分类调整解除申请审批流程失败!原因: " + e.getMessage());
		}
		return getResponseData(outputVo);
	}

	/**
	 * TODO: 风险分类调整 提交数据 发起流程
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2019/9/11 14:59
	 * @version 0.1
	 */
	@RequestMapping(value="/rsc/adjust/app/commit", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> appCommit(@RequestBody RscAdjustAppVO vo) {
		int outputVo = 0;
		setUserInfoToVO(vo);
		ResponseData<Integer> integerResponseData =null;
		if(null==vo.getRscAdjNo() || ""==vo.getRscAdjNo()){
			// 新增数据
			integerResponseData = insertRscAdjNo(vo);
		}else{
			// 修改数据
			integerResponseData = updateByRscAdjNo(vo);
		}
		if(!integerResponseData.getCode().equals("0")){
			outputVo = -1;
			return setResponseMessage(outputVo, integerResponseData.getMessage());
		}
		if(vo.getApprStatus().equals(ConsoleConstant.APRV_STATUS_05)){
			flowRepulseUtil.cleanRepulseAllInfo(vo.getRscAdjNo());
		}
		// 发起流程
		logger.info("即将发起额度限额审批流程，信息为:" + JSON.toJSONString(vo));
		// 事件ID
		String eventId = "mRscAdjustAppEnter";
		String flowId = "FW20190910000000001";

		vo.setApprStatus(ConsoleConstant.APRV_STATUS_02);

		BizFlowInfoVO bizFlowInfoVO = new BizFlowInfoVO();
		bizFlowInfoVO.setBizFlowType(FlowConstance.BIZ_FLOW_TYPE_09);
		bizFlowInfoVO.setBizFlowId(flowId);
		List<BizFlowInfoVO> listFlow = bizFlowInfoService.queryBizFlowInfoByFlowType(bizFlowInfoVO);

		// 获取流程ID
		logger.info("查询到的业务流程参数为："+ JSONObject.toJSONString(listFlow));
		if (Objects.nonNull(listFlow) && listFlow.size()>0) {
			bizFlowInfoVO = listFlow.get(0);
			flowId = bizFlowInfoVO.getBizFlowId();  // 得到业务流转ID
			vo.setApprStatus(ConsoleConstant.APRV_STATUS_02);
		} else {
			//提交失败 待发起
			vo.setApprStatus(ConsoleConstant.APRV_STATUS_01);
			rscAdjustAppService.updateByRscAdjNo(vo);
			outputVo = -1;
			return setResponseMessage(outputVo, "未配置相关信息提交失败!");
		}
		EngineHttpRequestVO eVo = new EngineHttpRequestVO();
		// 业务法人机构
		eVo.setBizLagelOrgCode(this.getUserInfo().getOrgInfo().getOrgCode());
		// 业务流水号
		eVo.setE_bizSerno(vo.getRscAdjNo());
		eVo.setFlowId(flowId);
		//事件ID
		eVo.setData(vo);
		eVo.setEventId(eventId);
		eVo.setG_bizSerno(vo.getRscAdjNo());
		if (!flowCommonService.dutyFlowCheck(eventId,flowId)){
			outputVo = -1;
			return setResponseMessage(outputVo, "发起风险分类调整申请审批流程失败!原因: 流程岗位已失效");
		}
		try {
			HttpClientUtil.callFlowEngine(flowUrlConfigService.getRestfulSyncStartEventUrl(), eVo, String.class);
			vo.setAppDate(pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001));
			outputVo = rscAdjustAppService.updateByRscAdjNo(vo);
		} catch (Exception e) {
			// 发起失败
			e.printStackTrace();
			logger.error("RscAdjustAppVO", e.getMessage());
			outputVo = -1;
			return setResponseMessage(outputVo, "发起风险分类调整申请审批流程失败!原因: " + e.getMessage());
		}
		return getResponseData(outputVo);
	}

	private ResponseData<Integer> setResponseMessage(Integer outputVo, String message) {
		ResponseData<Integer> responseData = new ResponseData<Integer>();
		responseData.setRows(outputVo);
		responseData.setMessage(message);
		responseData.setCode(ApplicationConstance.FAILURE);
		responseData.setSuccess(true);
		return responseData;
	}

	private String queryCurrResult(String billNo ){
		RscTaskLoanVO rscTaskLoanVO = new RscTaskLoanVO();
		rscTaskLoanVO.setBillNo(billNo);
		RscTaskLoanVO rscTaskLoanQ = rscTaskLoanService.queryByPk(rscTaskLoanVO);
		if (rscTaskLoanQ != null && !StringUtil.isNullorBank(rscTaskLoanQ.getCurrResult())){
			return rscTaskLoanQ.getCurrResult();
		}
		return null;
	}
}
